package cn.edu.ctbu.simplescoremanagementsystem.aspect;

import cn.edu.ctbu.simplescoremanagementsystem.constain.ResultEnum;
import cn.edu.ctbu.simplescoremanagementsystem.utils.ResultUtils;
import cn.edu.ctbu.simplescoremanagementsystem.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
@Aspect
@Slf4j
public class HttpAspect {
    @Pointcut("execution(public * cn.edu.ctbu.simplescoremanagementsystem.webapi.*.*(..))")
    public void log(){

    }
    @Before("log()")
    public void doBefore(){
        log.info("api执行开始....");
    }

    @After("log()")
    public void doAfter(){
        log.info("api执行结束....");
    }

    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object){
        log.info("response={}",object);
    }

    @Around("log()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();//计时开始
        Object obj = pjp.proceed(pjp.getArgs());//执行方法
        stopWatch.stop();//计时结束
        long cost = stopWatch.getTotalTimeMillis();
        MethodSignature signature = (MethodSignature) pjp.getSignature();//获取方法签名
        String methodName = signature.getDeclaringTypeName()+"."+signature.getName();//获取方法名
        log.info("-------------执行" + methodName + "方法，用时" + cost +"ms ---------------");
        return obj;
    }
}
